package com.yuuko.yuukoapi.project.limb.controller

import com.yuuko.yuukoapi.common.request.PageParam
import com.yuuko.yuukoapi.common.response.AjaxResult
import com.yuuko.yuukoapi.common.response.None
import com.yuuko.yuukoapi.framework.extension.findPage
import com.yuuko.yuukoapi.project.limb.doman.entity.DeviceModel
import com.yuuko.yuukoapi.project.limb.doman.entity.DeviceParam
import com.yuuko.yuukoapi.project.limb.doman.entity.dto.DeviceParamListSpec
import com.yuuko.yuukoapi.project.limb.doman.entity.dto.DeviceParamUpdateInput
import com.yuuko.yuukoapi.repository.limb.DeviceParamRepository
import com.yuuko.yuukoapi.repository.limb.DeviceRepository
import org.babyfish.jimmer.Page
import org.babyfish.jimmer.sql.ast.mutation.SaveMode
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/limb/device-param")
class DeviceParamController(
    val deviceParamRepository: DeviceParamRepository
) {
    @GetMapping("/list")
    fun getDeviceParamList(
        page: PageParam,
        spec: DeviceParamListSpec
    ): AjaxResult<Page<DeviceParam>> {
        val result = deviceParamRepository.findPage(page, spec)
        return AjaxResult.ok(result)
    }

    @PostMapping
    fun createDeviceParam(
        @RequestBody input: DeviceParamUpdateInput,
    ): AjaxResult<None?> {
        deviceParamRepository.save(input, SaveMode.INSERT_ONLY)
        return AjaxResult.ok()
    }

    @PutMapping("/{id}")
    fun updateDeviceParam(
        @PathVariable id: String,
        @RequestBody input: DeviceParamUpdateInput
    ): AjaxResult<None?> {
        deviceParamRepository.save(input.toEntity { this.id = id }, SaveMode.UPDATE_ONLY)
        return AjaxResult.ok()
    }

    @DeleteMapping("/{id}")
    fun deleteDeviceParam(@PathVariable id: String): AjaxResult<None?> {
        deviceParamRepository.deleteById(id)
        return AjaxResult.ok()
    }
}